package com.huika.dealontop.control.login.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.encrypt.MD5Security;
import com.huika.dealontop.R;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.control.login.event.LoginEvent;
import com.huika.dealontop.control.login.validations.PassWordValidation;
import com.huika.dealontop.support.domain.FindPwdVerfyBean;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.LoginUtils;
import com.huika.dealontop.views.TitleBarHelper;
import com.huika.dealontop.views.cleardit.ClearableEditText;
import com.huika.huikalib.validation.EditTextValidator;
import com.huika.huikalib.validation.ValidationModel;

import de.greenrobot.event.EventBus;

public class SetNewPwdActivity extends BaseAct implements Response.Listener<RequestResult<String>> {
    private ClearableEditText etPwd;
    private ClearableEditText etConPwd;
    private Button btn;
    private EditTextValidator editTextValidator;

    private FindPwdVerfyBean findPwdVerfyBean;
    private String phone;
    private String pwd;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_set_newpwd);
        EventBus.getDefault().register(this);
    }

    public void onEventMainThread(LoginEvent loginEvent) {
        if (loginEvent.getLoginStatus() == 1) {
            this.finish();
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        findPwdVerfyBean = (FindPwdVerfyBean) intent.getSerializableExtra("FindPwdVerfyBean");
        phone = intent.getStringExtra("phone");
    }

    @Override
    protected void initWidget() {
        TitleBarHelper titleBarHelper = new TitleBarHelper(this, -1, -1, R.string.title_set_newpwd);
        titleBarHelper.setLeftMsg(R.string.left_back);
        etPwd = (ClearableEditText) findViewById(R.id.et_pwd_asn);
        etConPwd = (ClearableEditText) findViewById(R.id.et_conpwd_asn);
        btn = (Button) findViewById(R.id.btn_asn);
        etPwd.addTextChangedListener(new MyPassWordWatcher(etPwd));
        etConPwd.addTextChangedListener(new MyPassWordWatcher(etConPwd));
        btn.setOnClickListener(this);
        editTextValidator = new EditTextValidator(this).add(new ValidationModel(etPwd, new PassWordValidation())).execute();
    }

    class MyPassWordWatcher implements TextWatcher {
        private EditText editText;
        private String tempStr;

        public MyPassWordWatcher(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (tempStr.indexOf(" ") != -1) {
                editText.setText(tempStr.trim());
                editText.setSelection(tempStr.length());
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.toString().equals(" ")) {
                editText.setText("");
            } else {
                tempStr = s.toString();
            }
        }

    }

    @Override
    public void widgetClick(View v) {
        net();
    }

    private void net() {
        if (editTextValidator.validate()) {
            String conPwdStr = getInputStr(etConPwd);
            pwd = getInputStr(etPwd);
            if (!pwd.equals(conPwdStr)) {
                showToastMsg("两次密码不一致！");
                return;
            }
            FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.FINDPASSWORDRESET, this, this, new TypeToken<RequestResult<String>>() {
            }.getType());
            JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
            ajaxParams.putStringTypeParam("userId", findPwdVerfyBean.getUid());
            ajaxParams.putStringTypeParam("phone", phone);
            ajaxParams.putStringTypeParam("key", findPwdVerfyBean.getKey());
            ajaxParams.putStringTypeParam("password", MD5Security.getMd5_32_UP(pwd));
//			ajaxParams.putStringTypeParam("ip", GlobalApp.ip);
//			ajaxParams.putStringTypeParam("mac", GlobalApp.mac);
//			ajaxParams.putStringTypeParam("dev", Constant.DEV);
            request.setRequestParams(ajaxParams);
            executeRequest(request);
        }
    }

    @Override
    public void onResponse(RequestResult<String> arg0) {
        dismissLoadingDialog();
        if (arg0.flag == 1) {
            LoginUtils loginUtils = new LoginUtils(this);
            loginUtils.login(phone, pwd);
        }
        showToastMsg(arg0.msg);
    }

}
